#!/bin/bash

vip_1=192.168.2.100
host_1_ip_0=192.168.2.73
host_1_ip_1=192.168.11.73

vip_2=192.168.11.100
host_2_ip_0=192.168.2.74
host_2_ip_1=192.168.11.74


wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
tar xvf libevent-2.1.8-stable.tar.gz -C /usr/local/src
cd /usr/local/src/libevent-2.1.8-stable
./configure --prefix=/usr/local/libevent
make && make install
cd


#wget http://memcached.org/files/memcached-1.5.12.tar.gz
#tar -zxvf memcached-1.5.12.tar.gz -C /usr/local/src
#cd /usr/local/src/memcached-1.5.12
# ./configure && make && make test && make install

# wget http://downloads.sourceforge.net/repcached/repcached-2.2.1-1.2.8.patch.gz
# yum install -y patch
# gzip -cd /root/repcached-2.2.1-1.2.8.patch.gz | patch -p1 -R

wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.1.tar.gz
tar xvf memcached-1.2.8-repcached-2.2.1.tar.gz -C /usr/local/src
cd /usr/local/src/memcached-1.2.8-repcached-2.2.1
sed -i '57d' memcached.c
sed -i '59d' memcached.c
./configure --prefix=/usr/local/repcached --enable-replication --with-libevent=/usr/local/libevent
make && make install
echo '/usr/local/libevent/lib' >/etc/ld.so.conf.d/libevent-2.1.conf
ldconfig -v | grep libevent


################################## mysql_1 ################################
# 配置memcache服务并设置从什么地方同步数据
/usr/local/repcached/bin/memcached -d -m 1024 -p 11211 -u root -c 65530 -x 192.168.2.74


################################## mysql_2 ################################
# 配置memcache服务并设置从什么地方同步数据
/usr/local/repcached/bin/memcached -d -m 1024 -p 11211 -u root -c 65530 -x 192.168.2.73


################################## ha_1, ha_2 ################################
echo """
listen  openstack_mysql_port
    bind 192.168.2.100:3306
    mode tcp
    log global
    server 192.168.2.73  192.168.2.73:3306  check inter 3000 fall 2 rise 5
#    server 192.168.2.74  192.168.2.74:3306  check inter 3000 fall 2 rise 5


listen  openstack_memcache_port
    bind 192.168.2.100:11211
    mode tcp
    log global
    server 192.168.2.73  192.168.2.73:11211  check inter 3000 fall 2 rise 5
    server 192.168.2.74  192.168.2.74:11211  check inter 3000 fall 2 rise 5
""" >>/etc/haproxy/conf/openstack.cfg 

systemctl restart haproxy